<?php

namespace app\admin\command;

use app\admin\model\share\CommonShare;
use app\admin\model\User;
use app\api\controller\Pay;
use app\common\model\AdvertPriceLog;
use app\common\model\MoneyLog;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use \app\admin\model\share\ShareUser;
use \app\admin\model\share\Money as ShareMoney;
use \app\admin\model\share\Reward as ShareReward;

class Income extends Command
{
    protected function configure()
    {
        $this->setName('Income')->setDescription("计划任务 Income");
    }

    protected function execute(Input $input, Output $output)
    {
        $output->writeln('Date Crontab job start...');
        /*** 这里写计划任务列表集 START ***/
        //删除测试账号
        (new ShareUser())->del_test_user(1);
        $pattern = Db::name("config")->where(['name'=>'pattern'])->value('value');
        if(isset($pattern) && $pattern == 2){
            $this->IncomeTwo();
        }elseif(isset($pattern) && $pattern == 3){
            $this->IncomeThree();
        }else{
            $this->Income();
        }
        $this->lpIncome();

        //查询昨天订单信息
        $this->updateYesterdayRecharge();

        //联创收益
        $this->_set_goods_reward();

        //校验网体数据
        (new ShareUser())->check_share();

        //校验数据
        /*** 这里写计划任务列表集 END ***/
        $output->writeln('Date Crontab job end...');
    }

    // 获取昨天实际支付订单
    public function Income()
    {
        //开始时间
        $start_time = date("Y-m-d", strtotime("-1 day"));
        //结束时间
        $url = Db::name("config")->where(['name' => 'url'])->find();
        if(isset($url) && ($url['value'] == 'https://y-sphj.bigchun.com' || $url['value'] == 'https://zhongjunf.bigchun.com')){
            $end_time = time();
        }else{
            $end_time = date('Y-m-d');
        }

//        $end_time = time();
        //删除测试收益
        $this->del_share_reward();
        $list = \db("share_user")->where('user_flags !=""')->select();
//        echo json_encode($list);
        foreach ($list as $v) {
            $this->single_team($v['user_flags'], $start_time, $end_time);
        }

        $ShareReward5 = (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where(['type' => 5, 'status' => 0])->where('user_flags = ""')->sum('money');
        $ShareReward4 = (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where(['type' => 4, 'status' => 0])->where('user_flags = ""')->sum('money');
        $ShareReward2 = (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where(['type' => 2, 'status' => 0])->where('user_flags = ""')->sum('money');
        $ShareReward1 = (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where(['type' => 1, 'status' => 0])->where('user_flags = ""')->sum('money');
        echo 'money:' . json_encode([$ShareReward5, $ShareReward4, $ShareReward2, $ShareReward1]) . PHP_EOL;

        $ShareUser1 = $this->getUnTeamCount(1);
        $ShareUser2 = $this->getUnTeamCount(2);
        $ShareUser3 = $this->getUnTeamCount(3);
        $ShareUser4 = $this->getUnTeamCount(4);
        echo 'count:' . json_encode([count($ShareUser1), count($ShareUser2), count($ShareUser3), count($ShareUser4)]) . PHP_EOL;

        $money1 = empty(count($ShareUser1)) ? 0 : bcdiv($ShareReward1, count($ShareUser1), 2);
        $money2 = empty(count($ShareUser2)) ? 0 : bcdiv($ShareReward2, count($ShareUser2), 2);
        $money4 = empty(count($ShareUser3)) ? 0 : bcdiv($ShareReward4, count($ShareUser3), 2);
        $money5 = empty(count($ShareUser4)) ? 0 : bcdiv($ShareReward5, count($ShareUser4), 2);

        if (floatval($money1) > 0) {
            foreach ($ShareUser1 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(3, $money1, $value['user_id'], $value['role']);
               //$value->price += bcmul($price_bl, $money1, 2);
                $value->advert_price += bcmul($price_bl, $money1, 2);
                $value->total_price += $money1;
                $value->mutual_money += bcmul($mutual_money_bl, $money1, 2);
                $value->save();
            }
        }

        if (floatval($money2) > 0) {
            foreach ($ShareUser2 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(3, $money2, $value['user_id'], $value['role']);
                //$value->price += bcmul($price_bl, $money2, 2);
                $value->advert_price += bcmul($price_bl, $money2, 2);
                $value->total_price += $money2;
                $value->mutual_money += bcmul($mutual_money_bl, $money2, 2);
                $value->save();
            }
        }

        if (floatval($money4) > 0) {
            foreach ($ShareUser3 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(3, $money4, $value['user_id'], $value['role']);
                //$value->price += bcmul($price_bl, $money4, 2);
                $value->advert_price += bcmul($price_bl, $money4, 2);
                $value->total_price += $money4;
                $value->mutual_money += bcmul($mutual_money_bl, $money4, 2);
                $value->save();
            }
        }

        if (floatval($money5) > 0) {
            foreach ($ShareUser4 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(3, $money5, $value['user_id'], $value['role']);
                //$value->price += bcmul($price_bl, $money5, 2);
                $value->advert_price += bcmul($price_bl, $money5, 2);
                $value->total_price += $money5;
                $value->mutual_money += bcmul($mutual_money_bl, $money5, 2);
                $value->save();
            }
        }

        (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where('type', 'in', array(1, 2, 4, 5))->where(['status' => 0])->where('user_flags = ""')->update(['status' => 1]);
        // 判断是否是周一
        $week = date("w");
        if ($week == "1") {
            // 上周一时间
            $week_time = date("Y-m-d", strtotime("-7 day"));
            $ShareReward3 = (new ShareReward())->where('addtime', 'between time', [$week_time, $end_time])->where(['type' => 3, 'status' => 0])->where('user_flags =""')->sum('money');
            $money3 = empty(count($ShareUser3)) ? 0 : bcdiv($ShareReward3, count($ShareUser3), 2);
            foreach ($ShareUser3 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(4, $money3, $value['user_id'], $value['role'], $value['user_flags']);
                //$value->price += bcmul($price_bl, $money3, 2);
                $value->advert_price += bcmul($price_bl, $money3, 2);
                $value->total_price += $money3;
                $value->mutual_money += bcmul($mutual_money_bl, $money3, 2);
                $value->save();
            }
            (new ShareReward())->where('addtime', 'between time', [$week_time, $end_time])->where(['type' => 3, 'status' => 0])->where('user_flags =""')->update(['status' => 1]);
        }

        echo date("Y-m-d H:i:s") . "成功 \r\n";
    }
    public function IncomeTwo()
    {
        //开始时间
        $start_time = date("Y-m-d", strtotime("-1 day"));
        //结束时间
        $url = Db::name("config")->where(['name' => 'url'])->find();
        if(isset($url) && ($url['value'] == 'https://y-sphj.bigchun.com' || $url['value'] == 'https://zhongjunf.bigchun.com')){
            $end_time = time();
        }else{
            $end_time = date('Y-m-d');
        }

//        $end_time = time();
        //删除测试收益
        $this->del_share_reward();
        $list = \db("share_user")->where('user_flags !=""')->select();
//        echo json_encode($list);
        foreach ($list as $v) {
            $this->single_team_two($v['user_flags'], $start_time, $end_time);
        }

        $ShareReward13 = (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where(['type' => 13, 'status' => 0])->where('user_flags = ""')->sum('money');
        $ShareReward12 = (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where(['type' => 12, 'status' => 0])->where('user_flags = ""')->sum('money');
        $ShareReward11 = (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where(['type' => 11, 'status' => 0])->where('user_flags = ""')->sum('money');
        $ShareReward10 = (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where(['type' => 10, 'status' => 0])->where('user_flags = ""')->sum('money');
        $ShareReward9 = (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where(['type' => 9, 'status' => 0])->where('user_flags = ""')->sum('money');
        $ShareReward8 = (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where(['type' => 8, 'status' => 0])->where('user_flags = ""')->sum('money');
        $ShareReward7 = (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where(['type' => 7, 'status' => 0])->where('user_flags = ""')->sum('money');
        echo 'money:' . json_encode([$ShareReward13, $ShareReward12, $ShareReward11, $ShareReward10, $ShareReward9, $ShareReward8, $ShareReward7]) . PHP_EOL;

        $ShareUser7 = $this->getUnTeamCount(1);
        $ShareUser8 = $this->getUnTeamCount(2);
        $ShareUser9 = $this->getUnTeamCount(3);
        $ShareUser10 = $this->getUnTeamCount(4);
        $ShareUser11 = $this->getUnTeamCount(5);
        $ShareUser12 = $this->getUnTeamCount(6);
        $ShareUser13 = $this->getUnTeamCount(7);
        echo 'count:' . json_encode([count($ShareUser7), count($ShareUser8), count($ShareUser9), count($ShareUser10), count($ShareUser11), count($ShareUser12), count($ShareUser13)]) . PHP_EOL;

        $money7 = empty(count($ShareUser7)) ? 0 : bcdiv($ShareReward7, count($ShareUser7), 2);
        $money8 = empty(count($ShareUser8)) ? 0 : bcdiv($ShareReward8, count($ShareUser8), 2);
        $money9 = empty(count($ShareUser9)) ? 0 : bcdiv($ShareReward9, count($ShareUser9), 2);
        $money10 = empty(count($ShareUser10)) ? 0 : bcdiv($ShareReward10, count($ShareUser10), 2);
        $money11 = empty(count($ShareUser11)) ? 0 : bcdiv($ShareReward11, count($ShareUser11), 2);
        $money12 = empty(count($ShareUser12)) ? 0 : bcdiv($ShareReward12, count($ShareUser13), 2);
        $money13 = empty(count($ShareUser13)) ? 0 : bcdiv($ShareReward13, count($ShareUser13), 2);

        if (floatval($money7) > 0) {
            foreach ($ShareUser7 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(7, $money7, $value['user_id'], $value['role']);
                //$value->price += bcmul($price_bl, $money1, 2);
                $value->advert_price += bcmul($price_bl, $money7, 2);
                $value->total_price += $money7;
                $value->mutual_money += bcmul($mutual_money_bl, $money7, 2);
                $value->save();
            }
        }

        if (floatval($money8) > 0) {
            foreach ($ShareUser8 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(7, $money8, $value['user_id'], $value['role']);
                //$value->price += bcmul($price_bl, $money2, 2);
                $value->advert_price += bcmul($price_bl, $money8, 2);
                $value->total_price += $money8;
                $value->mutual_money += bcmul($mutual_money_bl, $money8, 2);
                $value->save();
            }
        }

        if (floatval($money9) > 0) {
            foreach ($ShareUser9 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(7, $money9, $value['user_id'], $value['role']);
                //$value->price += bcmul($price_bl, $money4, 2);
                $value->advert_price += bcmul($price_bl, $money9, 2);
                $value->total_price += $money9;
                $value->mutual_money += bcmul($mutual_money_bl, $money9, 2);
                $value->save();
            }
        }

        if (floatval($money10) > 0) {
            foreach ($ShareUser10 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(7, $money10, $value['user_id'], $value['role']);
                //$value->price += bcmul($price_bl, $money5, 2);
                $value->advert_price += bcmul($price_bl, $money10, 2);
                $value->total_price += $money10;
                $value->mutual_money += bcmul($mutual_money_bl, $money10, 2);
                $value->save();
            }
        }
        if (floatval($money11) > 0) {
            foreach ($ShareUser11 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(7, $money11, $value['user_id'], $value['role']);
                //$value->price += bcmul($price_bl, $money5, 2);
                $value->advert_price += bcmul($price_bl, $money11, 2);
                $value->total_price += $money11;
                $value->mutual_money += bcmul($mutual_money_bl, $money11, 2);
                $value->save();
            }
        }
        if (floatval($money12) > 0) {
            foreach ($ShareUser12 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(7, $money12, $value['user_id'], $value['role']);
                //$value->price += bcmul($price_bl, $money5, 2);
                $value->advert_price += bcmul($price_bl, $money12, 2);
                $value->total_price += $money12;
                $value->mutual_money += bcmul($mutual_money_bl, $money12, 2);
                $value->save();
            }
        }
        if (floatval($money13) > 0) {
            foreach ($ShareUser13 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(7, $money13, $value['user_id'], $value['role']);
                //$value->price += bcmul($price_bl, $money5, 2);
                $value->advert_price += bcmul($price_bl, $money13, 2);
                $value->total_price += $money13;
                $value->mutual_money += bcmul($mutual_money_bl, $money13, 2);
                $value->save();
            }
        }
        (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where('type', 'in', array(7, 8, 9, 10, 11, 12, 13))->where(['status' => 0])->where('user_flags = ""')->update(['status' => 1]);

        echo date("Y-m-d H:i:s") . "成功 \r\n";
    }
    public function IncomeThree()
    {
        //开始时间
        $start_time = date("Y-m-d", strtotime("-1 day"));
        //结束时间
        $url = Db::name("config")->where(['name' => 'url'])->find();
        if(isset($url) && ($url['value'] == 'https://y-sphj.bigchun.com' || $url['value'] == 'https://zhongjunf.bigchun.com')){
            $end_time = time();
        }else{
            $end_time = date('Y-m-d');
        }

//        $end_time = time();
        //删除测试收益
        $this->del_share_reward();
        $list = \db("share_user")->where('user_flags !=""')->select();
//        echo json_encode($list);
        foreach ($list as $v) {
            $this->single_team_three($v['user_flags'], $start_time, $end_time);
        }


        $ShareReward7 = (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where(['type' => 7, 'status' => 0])->where('user_flags = ""')->sum('money');
        echo 'money:' . json_encode([$ShareReward7]) . PHP_EOL;

        $ShareUser7 = $this->getUnTeamCount(1);

        echo 'count:' . json_encode([count($ShareUser7)]) . PHP_EOL;

        $money7 = empty(count($ShareUser7)) ? 0 : bcdiv($ShareReward7, count($ShareUser7), 2);


        if (floatval($money7) > 0) {
            foreach ($ShareUser7 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(7, $money7, $value['user_id'], $value['role']);
                //$value->price += bcmul($price_bl, $money1, 2);
                $value->advert_price += bcmul($price_bl, $money7, 2);
                $value->total_price += $money7;
                $value->mutual_money += bcmul($mutual_money_bl, $money7, 2);
                $value->save();
            }
        }
        (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where('type', 'in', array(7))->where(['status' => 0])->where('user_flags = ""')->update(['status' => 1]);

        echo date("Y-m-d H:i:s") . "成功 \r\n";
    }
    /**
     * 删除测试收益记录
     */
    public function del_share_reward(){
        //开始时间
        $start_time = date("Y-m-d", strtotime("-1 week"));
        //结束时间
       $end_time = date('Y-m-d');
        $ShareRewardList = (new ShareReward())
            ->where('addtime', 'between time', [$start_time, $end_time])
            ->where(['status' => 0])
            ->where('order_id','>',0)
            ->group("order_id")
            ->select();
        foreach ($ShareRewardList as $k=>$v){
            $order_data = Db::name("shopro_order")->where(['id'=>$v['order_id']])->find();
            if(!$order_data){//订单不存在删除
                (new ShareReward())->where(['id'=>$v['id'],'status' => 0])->delete();
                continue;
            }
            $user_data = Db::name("user")->where(['id'=>$order_data['user_id']])->find();
            if(!$user_data){//用户不存在删除
                (new ShareReward())->where(['id'=>$v['id'],'status' => 0])->delete();
                continue;
            }
            if($user_data['staff'] == 2){//测试用户删除
                (new ShareReward())->where(['id'=>$v['id'],'status' => 0])->delete();
                continue;
            }
        }
        return true;
    }
    private function single_team($user_flags, $start_time, $end_time)
    {
        //总金额
        $ShareReward5 = (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where(['type' => 5, 'status' => 0, 'user_flags' => $user_flags])->sum('money');
        $ShareReward4 = (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where(['type' => 4, 'status' => 0, 'user_flags' => $user_flags])->sum('money');
        $ShareReward2 = (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where(['type' => 2, 'status' => 0, 'user_flags' => $user_flags])->sum('money');
        $ShareReward1 = (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where(['type' => 1, 'status' => 0, 'user_flags' => $user_flags])->sum('money');

        echo $user_flags . '   money:' . json_encode([$ShareReward5, $ShareReward4, $ShareReward2, $ShareReward1]) . PHP_EOL;
        //总人数
        $ShareUser1 = $this->getTeamCount($user_flags, 1);
        $ShareUser2 = $this->getTeamCount($user_flags, 2);
        $ShareUser3 = $this->getTeamCount($user_flags, 3);
        $ShareUser4 = $this->getTeamCount($user_flags, 4);
        echo $user_flags . '   count:' . json_encode([count($ShareUser1), count($ShareUser2), count($ShareUser3), count($ShareUser4)]) . PHP_EOL;

        $money1 = empty(count($ShareUser1)) ? 0 : bcdiv($ShareReward1, count($ShareUser1), 2);
        $money2 = empty(count($ShareUser2)) ? 0 : bcdiv($ShareReward2, count($ShareUser2), 2);
        $money4 = empty(count($ShareUser3)) ? 0 : bcdiv($ShareReward4, count($ShareUser3), 2);
        $money5 = empty(count($ShareUser4)) ? 0 : bcdiv($ShareReward5, count($ShareUser4), 2);

        if (floatval($money1) > 0) {
            foreach ($ShareUser1 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(3, $money1, $value['user_id'], $value['role'], $user_flags);
                //$value->price += bcmul($price_bl, $money1, 2);
                $value->advert_price += bcmul($price_bl, $money1, 2);
                $value->total_price += $money1;
                $value->mutual_money += bcmul($mutual_money_bl, $money1, 2);
//            echo json_encode($value) . PHP_EOL;
                $value->save();
            }
        }

        if (floatval($money2) > 0) {
            foreach ($ShareUser2 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(3, $money2, $value['user_id'], $value['role'], $user_flags);
                //$value->price += bcmul($price_bl, $money2, 2);
                $value->advert_price += bcmul($price_bl, $money2, 2);
                $value->total_price += $money2;
                $value->mutual_money += bcmul($mutual_money_bl, $money2, 2);
                $value->save();
            }
        }

        if (floatval($money4) > 0) {
            foreach ($ShareUser3 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(3, $money4, $value['user_id'], $value['role'], $user_flags);
               //$value->price += bcmul($price_bl, $money4, 2);
                $value->advert_price += bcmul($price_bl, $money4, 2);
                $value->total_price += $money4;
                $value->mutual_money += bcmul($mutual_money_bl, $money4, 2);
                $value->save();
            }
        }

        if (floatval($money5) > 0) {
            foreach ($ShareUser4 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(3, $money5, $value['user_id'], $value['role'], $user_flags);
               //$value->price += bcmul($price_bl, $money5, 2);
                $value->advert_price += bcmul($price_bl, $money5, 2);
                $value->total_price += $money5;
                $value->mutual_money += bcmul($mutual_money_bl, $money5, 2);
                $value->save();
            }
        }


        (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where('type', 'in', array(1, 2, 4, 5))->where('user_flags', $user_flags)->where(['status' => 0])->update(['status' => 1]);
        // 判断是否是周一
        $week = date("w");
        if ($week == "1") {
            // 上周一时间
            $week_time = date("Y-m-d", strtotime("-7 day"));
            $ShareReward3 = (new ShareReward())->where('addtime', 'between time', [$week_time, $end_time])->where(['type' => 3, 'status' => 0])->where('user_flags', $user_flags)->sum('money');
            $money3 = empty(count($ShareUser3)) ? 0 : bcdiv($ShareReward3, count($ShareUser3), 2);
            foreach ($ShareUser3 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(4, $money3, $value['user_id'], $value['role'], $value['user_flags']);
                //$value->price += bcmul($price_bl, $money3, 2);
                $value->advert_price += bcmul($price_bl, $money3, 2);
                $value->total_price += $money3;
                $value->mutual_money += bcmul($mutual_money_bl, $money3, 2);
                $value->save();
            }
            (new ShareReward())->where('addtime', 'between time', [$week_time, $end_time])->where(['type' => 3, 'status' => 0])->where('user_flags', $user_flags)->update(['status' => 1]);
        }

    }
    private function single_team_two($user_flags, $start_time, $end_time)
    {
        //总金额
        $ShareReward7 = (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where(['type' => 13, 'status' => 0, 'user_flags' => $user_flags])->sum('money');
        $ShareReward6 = (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where(['type' => 12, 'status' => 0, 'user_flags' => $user_flags])->sum('money');
        $ShareReward5 = (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where(['type' => 11, 'status' => 0, 'user_flags' => $user_flags])->sum('money');
        $ShareReward4 = (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where(['type' => 10, 'status' => 0, 'user_flags' => $user_flags])->sum('money');
        $ShareReward3 = (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where(['type' => 9, 'status' => 0, 'user_flags' => $user_flags])->sum('money');
        $ShareReward2 = (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where(['type' => 8, 'status' => 0, 'user_flags' => $user_flags])->sum('money');
        $ShareReward1 = (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where(['type' => 7, 'status' => 0, 'user_flags' => $user_flags])->sum('money');

        echo $user_flags . '   money:' . json_encode([$ShareReward7,$ShareReward6,$ShareReward5,$ShareReward4, $ShareReward3, $ShareReward2, $ShareReward1]) . PHP_EOL;
        //总人数
        $ShareUser1 = $this->getTeamCount($user_flags, 1);
        $ShareUser2 = $this->getTeamCount($user_flags, 2);
        $ShareUser3 = $this->getTeamCount($user_flags, 3);
        $ShareUser4 = $this->getTeamCount($user_flags, 4);
        $ShareUser5 = $this->getTeamCount($user_flags, 5);
        $ShareUser6 = $this->getTeamCount($user_flags, 6);
        $ShareUser7 = $this->getTeamCount($user_flags, 7);
        echo $user_flags . '   count:' . json_encode([count($ShareUser1), count($ShareUser2), count($ShareUser3), count($ShareUser4), count($ShareUser5), count($ShareUser6), count($ShareUser7)]) . PHP_EOL;

        $money1 = empty(count($ShareUser1)) ? 0 : bcdiv($ShareReward1, count($ShareUser1), 2);
        $money2 = empty(count($ShareUser2)) ? 0 : bcdiv($ShareReward2, count($ShareUser2), 2);
        $money3 = empty(count($ShareUser3)) ? 0 : bcdiv($ShareReward3, count($ShareUser3), 2);
        $money4 = empty(count($ShareUser4)) ? 0 : bcdiv($ShareReward4, count($ShareUser4), 2);
        $money5 = empty(count($ShareUser5)) ? 0 : bcdiv($ShareReward5, count($ShareUser5), 2);
        $money6 = empty(count($ShareUser6)) ? 0 : bcdiv($ShareReward6, count($ShareUser6), 2);
        $money7 = empty(count($ShareUser7)) ? 0 : bcdiv($ShareReward7, count($ShareUser7), 2);

        if (floatval($money1) > 0) {
            foreach ($ShareUser1 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(7, $money1, $value['user_id'], $value['role'], $user_flags);
                //$value->price += bcmul($price_bl, $money1, 2);
                $value->advert_price += bcmul($price_bl, $money1, 2);
                $value->total_price += $money1;
                $value->mutual_money += bcmul($mutual_money_bl, $money1, 2);
//            echo json_encode($value) . PHP_EOL;
                $value->save();
            }
        }

        if (floatval($money2) > 0) {
            foreach ($ShareUser2 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(7, $money2, $value['user_id'], $value['role'], $user_flags);
                //$value->price += bcmul($price_bl, $money2, 2);
                $value->advert_price += bcmul($price_bl, $money2, 2);
                $value->total_price += $money2;
                $value->mutual_money += bcmul($mutual_money_bl, $money2, 2);
                $value->save();
            }
        }

        if (floatval($money3) > 0) {
            foreach ($ShareUser3 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(3, $money3, $value['user_id'], $value['role'], $user_flags);
                //$value->price += bcmul($price_bl, $money4, 2);
                $value->advert_price += bcmul($price_bl, $money3, 2);
                $value->total_price += $money3;
                $value->mutual_money += bcmul($mutual_money_bl, $money3, 2);
                $value->save();
            }
        }

        if (floatval($money4) > 0) {
            foreach ($ShareUser4 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(3, $money4, $value['user_id'], $value['role'], $user_flags);
                //$value->price += bcmul($price_bl, $money5, 2);
                $value->advert_price += bcmul($price_bl, $money4, 2);
                $value->total_price += $money4;
                $value->mutual_money += bcmul($mutual_money_bl, $money4, 2);
                $value->save();
            }
        }

        if (floatval($money5) > 0) {
            foreach ($ShareUser5 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(3, $money5, $value['user_id'], $value['role'], $user_flags);
                //$value->price += bcmul($price_bl, $money5, 2);
                $value->advert_price += bcmul($price_bl, $money5, 2);
                $value->total_price += $money5;
                $value->mutual_money += bcmul($mutual_money_bl, $money5, 2);
                $value->save();
            }
        }
        if (floatval($money6) > 0) {
            foreach ($ShareUser6 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(3, $money6, $value['user_id'], $value['role'], $user_flags);
                //$value->price += bcmul($price_bl, $money5, 2);
                $value->advert_price += bcmul($price_bl, $money6, 2);
                $value->total_price += $money6;
                $value->mutual_money += bcmul($mutual_money_bl, $money6, 2);
                $value->save();
            }
        }
        if (floatval($money7) > 0) {
            foreach ($ShareUser7 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(3, $money7, $value['user_id'], $value['role'], $user_flags);
                //$value->price += bcmul($price_bl, $money5, 2);
                $value->advert_price += bcmul($price_bl, $money7, 2);
                $value->total_price += $money7;
                $value->mutual_money += bcmul($mutual_money_bl, $money7, 2);
                $value->save();
            }
        }

        (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where('type', 'in', array(7, 8, 9, 10,11,12,13))->where('user_flags', $user_flags)->where(['status' => 0])->update(['status' => 1]);

    }
    private function single_team_three($user_flags, $start_time, $end_time)
    {
        //总金额

        $ShareReward1 = (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where(['type' => 7, 'status' => 0, 'user_flags' => $user_flags])->sum('money');

        echo $user_flags . '   money:' . json_encode([ $ShareReward1]) . PHP_EOL;
        //总人数
        $ShareUser1 = $this->getTeamCount($user_flags, 1);

        echo $user_flags . '   count:' . json_encode([count($ShareUser1)]) . PHP_EOL;

        $money1 = empty(count($ShareUser1)) ? 0 : bcdiv($ShareReward1, count($ShareUser1), 2);

        if (floatval($money1) > 0) {
            foreach ($ShareUser1 as &$value) {
                //获取不通等级分给佣金的比例 qi11.3
                $con_ddata = Db::name("share_config")->where(['role' => $value['role'], 'type' => 3])->find();
                $price_bl = 1 - $con_ddata['reward'];
                $mutual_money_bl = $con_ddata['reward'];

                $this->add_money(7, $money1, $value['user_id'], $value['role'], $user_flags);
                //$value->price += bcmul($price_bl, $money1, 2);
                $value->advert_price += bcmul($price_bl, $money1, 2);
                $value->total_price += $money1;
                $value->mutual_money += bcmul($mutual_money_bl, $money1, 2);
//            echo json_encode($value) . PHP_EOL;
                $value->save();
            }
        }
        (new ShareReward())->where('addtime', 'between time', [$start_time, $end_time])->where('type', 'in', array(7))->where('user_flags', $user_flags)->where(['status' => 0])->update(['status' => 1]);

    }
    private function getTeamCount($user_flag, $role)
    {
        $users = \db('share_user s')->join('share_user_flags f', 'f.user_id = s.user_id')
            ->where('s.role', $role)
            ->where('f.user_flags', $user_flag)
            ->field('s.user_id')
            ->select();
        $user_ids = array_column($users, 'user_id');

        return (new ShareUser())->where('role', $role)->where('user_id', 'in', $user_ids)->select();
    }

    private function getUnTeamCount($role)
    {
        $teamUser1 = \db('share_user_flags f')->join('share_user s', 'f.user_id = s.user_id')
            ->where('s.role', $role)
            ->field('s.user_id')->select();
        $arr = array_column($teamUser1, 'user_id');

        return (new ShareUser())->where('role', $role)->where('user_id', 'not in', $arr)->select();
    }

    // type 1 直推 2极差 3日创收 4周创收
    private function add_money($type, $money, $user_id, $role, $user_flags = '')
    {
        if (!empty($money) && $money != "0.00") {
            //获取不通等级分给佣金的比例 qi11.3
            $con_ddata = Db::name("share_config")->where(['role' => $role, 'type' => 3])->find();
            //$user->price += bcmul(0.9, $money, 2);
            $price_bl = 1 - $con_ddata['reward'];
            $mutual_money_bl = $con_ddata['reward'];

            $ShareMoney = new ShareMoney();
            //$ShareMoney->money = round($money, 2);
            $ShareMoney->money = bcmul($price_bl, $money, 2);
            $ShareMoney->type = $type;
            $ShareMoney->order_id = 0;
            $ShareMoney->user_id = $user_id;
            $ShareMoney->goods_id = 0;
            $ShareMoney->role = $role;
            $ShareMoney->addtime = time();
            $ShareMoney->user_flags = $user_flags;
            $ShareMoney->mutual_money = bcmul($mutual_money_bl, $money, 2);
            $ShareMoney->save();

            $user = Db::name("share_user")->where(['user_id' => $user_id])->find();// 分销用户
            $after = $user['price'] + bcmul($price_bl, $money, 2);

            if($type == 3){
                $log_type = 13;//日广告收益
                $advert_type = 1;
                $memo = '日广告收益';
            }elseif ($type == 4){
                $log_type = 14;//周广告收益
                $advert_type = 2;
                $memo = '周广告收益';
            }elseif($type > 6){
                $advert_type = 1;
                $memo = '日广告收益';
            }
            $log_type = 3;
            if($type == 3 || $type == 4 || $type > 6){//广告创收
                $after = $user['advert_price'] + bcmul($price_bl, $money, 2);
                AdvertPriceLog::create(['user_id' => $user_id, 'money' => bcmul($price_bl, $money, 2), 'before' => $user['advert_price'], 'after' => $after, 'type' => $advert_type, 'memo' => $memo]);
            }else {
                MoneyLog::create(['user_id' => $user_id, 'money' => bcmul($price_bl, $money, 2), 'before' => $user['price'], 'after' => $after, 'type' => $log_type, 'memo' => "广告收益",]);
            }
            if (bcmul($mutual_money_bl, $money, 2) > 0) {
                $old_mutual_money = $user['mutual_money'];
                $param = [
                    "user_id" => $user_id,
                    "wallet" => bcmul($mutual_money_bl, $money, 2),       // 符号直接存到记录里面
                    "before" => $old_mutual_money,
                    "after" => $old_mutual_money + bcmul($mutual_money_bl, $money, 2),
                    "type" => 'mutual_add',
                    "memo" => '互助金增加',
                    "item_id" => 0,
                    "wallet_type" => 'mutual',
                    "ext" => [],
                    "oper_type" => 'system',
                    "oper_id" => 0,
                    "createtime" => time()
                ];
                Db::name('shopro_user_wallet_log')->insert($param);
            }

        }
    }

    private function updateYesterdayRecharge()
    {
        $yesterdaystart = strtotime(date('Ymd', strtotime('-1 day')));
        $yesterdayend = 86399 + $yesterdaystart;
        (new Pay)->updateRecharge(['createtime' => [['egt', $yesterdaystart], ['elt', $yesterdayend]]]);
        // (new Pay)->updateRecharge();
        echo date("Y-m-d H:i:s") . "restoreQuota 成功 \r\n";
    }

    private function _set_goods_reward(){
        $time = date('Y-m-d');
        $startDay = date('Y-m-d', strtotime("$time -1 day"));
        $monthStart = strtotime($startDay);
        $monthEnd = strtotime(date('Y-m-d', strtotime("$startDay +1 day")));

        $info = Db::table('share_goods_reward')->where(['month'=>date('Y-m-d', $monthStart)])->find();
        if(!empty($info)){
            return ['code'=>2, 'msg'=>'昨天收益已生成，请勿重复操作'];
        }
        $userList = Db::table('share_user')->where(['is_union'=>1])->select();
        if(empty($userList)){
            return ['code'=>4, 'msg'=>'暂无联创伙伴用户！'];
        }

        $yestoday_num_sum = $this->rewardMoney("yesterday");
        if($yestoday_num_sum == 0){
            return "无订单";
        }

        $amount = sprintf('%.2f', $yestoday_num_sum / count($userList));
        if(!empty($userList) && $amount > 0){
            foreach ($userList as $key=>$val){
//                $bankInfo = Db::table('shopro_user_bank')->where(['user_id'=>$val['user_id']])->find();
//                $data[$key]['real_name'] = isset($bankInfo['real_name']) ? $bankInfo['real_name'] : '';
//                $data[$key]['card_no'] = isset($bankInfo['card_no']) ? $bankInfo['card_no'] : '';
//                $data[$key]['bank_name'] = isset($bankInfo['bank_name']) ? $bankInfo['bank_name'] : '';
                $data[$key]['user_id'] = $val['user_id'];
                $data[$key]['money'] = $amount;
                $data[$key]['status'] = 1;
                $data[$key]['month'] = date('Y-m-d', $monthStart);
                $data[$key]['addtime'] = time();
            }
            $res = Db::table('share_goods_reward')->insertAll($data);
            if($res){
                return ['code'=>1, 'msg'=>'操作成功'];
            }
        }
        return ['code'=>0, 'msg'=>'操作失败'];
    }
    public function rewardMoney($day=''){

        $whereSql['one_day'] = ['IN', [0,5]];
        $whereSql['status'] = ['IN', [1, 2]];
        $whereSql['pay_fee'] = ['>', 10];
        $whereSql['share_retreat'] = 0;
        if($day){
            $mon_count = Db::table('shopro_order')->field("count(id) as order_count,goods_id")->whereTime('paytime', $day)->group("goods_id")->where($whereSql)->select();
        }else{
            $mon_count = Db::table('shopro_order')->field("count(id) as order_count,goods_id")->where($whereSql)->group("goods_id")->select();
        }

        $creation_money_sum = 0;
        foreach ($mon_count as $k=>$v){
            $creation_money = Db::name("shopro_goods")->where(['id'=>$v['goods_id']])->value("creation_money");
            $money = $creation_money*$v["order_count"];
            $creation_money_sum += $money;
        }
        return round($creation_money_sum,2);
    }
    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 批发零售收益发放
     * qi11.9
     */
    public function lpIncome(){
        //开始时间
        $start_time = date("Y-m-d", strtotime("-1 day"));
        //结束时间
//        $end_time = date('Y-m-d');
        $url = Db::name("config")->where(['name' => 'url'])->find();
        if(isset($url) && ($url['value'] == 'https://y-sphj.bigchun.com' || $url['value'] == 'https://zhongjunf.bigchun.com')){
            $end_time = time();
        }else{
            $end_time = date('Y-m-d');
        }
        $list = (new ShareMoney())
            ->where('addtime', 'between time', [$start_time, $end_time])
            ->where(['is_grant'=>0])
            ->where('type','in',[1,2])
            ->select();
        Db::startTrans();
        foreach ($list as $k=>$v){
            $order_data = Db::name("shopro_order")->where(['id'=>$v['order_id']])->find();
            if(!$order_data){//订单不存在删除
                (new ShareMoney())->where(['id'=>$v['id']])->delete();
                continue;
            }
            $user_data = Db::name("user")->where(['id'=>$order_data['user_id']])->find();
            if(!$user_data){//用户不存在删除
                (new ShareMoney())->where(['id'=>$v['id']])->delete();
                continue;
            }
            if($user_data['staff'] == 2){//测试用户删除
                (new ShareMoney())->where(['id'=>$v['id']])->delete();
                continue;
            }
            $share_data = (new ShareUser())->where(['user_id'=>$v['user_id']])->find();

            //互助金日志
            if($v['mutual_money'] > 0){
                //$save1 = \addons\shopro\model\User::mutual($v['mutual_money'], $v['user_id'], 'mutual_add', $v['order_id'], '', [], $share_data['mutual_money']);
                $old_mutual_money = $share_data['mutual_money'];
                $param = [
                    "user_id" => $v['user_id'],
                    "wallet" => $v['mutual_money'],       // 符号直接存到记录里面
                    "before" => $old_mutual_money,
                    "after" => $old_mutual_money + $v['mutual_money'],
                    "type" => 'mutual_add',
                    "memo" => '互助金增加',
                    "item_id" => 0,
                    "wallet_type" => 'mutual',
                    "ext" => [],
                    "oper_type" => 'system',
                    "oper_id" => 0,
                    "createtime" => time()
                ];
                $save1 = Db::name('shopro_user_wallet_log')->insert($param);
            }else{
                $save1 = true;
            }

            //余额日志
            $cz_type = 0;
            if ($v['type'] == 1) {
                $memo = '零售收益';
                $cz_type = 10;
            } elseif ($v['type'] == 2) {
                $memo = '批发收益';
                $cz_type = 11;
            }
            $after = $share_data['lp_price']+$v['money'];
            $save2 = MoneyLog::create(['user_id' =>  $v['user_id'], 'money' => $v['money'], 'before' =>$share_data['lp_price'], 'after' => $after, 'type' => $cz_type, 'memo' => $memo, 'order_id'=>$v['order_id'], 'is_old'=>2]);

            $share_data->lp_price += $v['money'];
            $share_data->total_price += $v['money']+$v['mutual_money'];
            $share_data->mutual_money += $v['mutual_money'];
            $save3 = $share_data->save();
            $save4 = (new ShareMoney())->where(['id'=>$v['id']])->update(['is_grant'=>1]);
            if(!$save1 || !$save2 || !$save3 || !$save4){
                Db::rollback();
            }
        }
        Db::commit();
        echo date("Y-m-d H:i:s") . "成功 \r\n";
    }

    /**
     * @return void
     * 添加好友关系
     */
    public function userFriends(){
        echo "添加好友开始";
        $share_list = Db::name("share_user")->select();
        foreach ($share_list as $k=>$v){
            //添加下级
            if ($v['user_id'] == 1) {
                $s = '%/' . $v['user_id'];
            } else {
                $s = '%/' . $v['user_id'] . '/%';
            }
            $sonShareAll = Db::name("share_user s")
                ->field("s.user_id,u.nickname,u.mobile,u.avatar,s.role")
                ->where('s.role_url', 'like', $s)
                ->join("user u"," u.id = s.user_id")
                ->where(['s.is_delete' => 0])
                ->where('s.role > 0')
                ->select();
            $in_arr = [];
            if($sonShareAll && count($sonShareAll) > 0){
                foreach ($sonShareAll as $k1=>$v1){
                   $f_data = Db::name("user_friends")->where(['user_id'=>$v['user_id'],'f_uid'=>$v1['user_id']])->find();
                    if($f_data) {
                        Db::name("user_friends")->where(['f_uid'=>$v1['user_id']])->update(['f_role'=>$v1['role']]);
                        continue;
                    }
                    $in_add = [
                        "user_id"=>  $v['user_id'],
                        "f_uid"=>  $v1['user_id'],
                        "f_nickname"=>  $v1['nickname'],
                        "f_mobile"=>  $v1['mobile'],
                        "f_status"=>  1,
                        "relation"=>  1,
                        "createtime"=>  time(),
                        "f_avatar"=>  $v1['avatar'],
                        "f_role"=>  $v1['role'],
                    ];
                    $in_arr[] = $in_add;
                }
                Db::name("user_friends")->insertAll($in_arr);
            }

            //添加上级
            $low_arr = explode('/',trim($v['role_url'],'/'));
            $up_list = Db::name("user")->where('id','in',$low_arr)->field('id,nickname,mobile,avatar')->select();
            if($up_list && count($up_list) > 0){
                $up_in_arr = [];
                foreach ($up_list as $k12=>$v2){
                    $f_data = Db::name("user_friends")->where(['user_id'=>$v['user_id'],'f_uid'=>$v2['id']])->find();

                    $share_data = Db::name("share_user")->where(['user_id'=>$v2['id']])->find();
                    if($f_data) {
                        Db::name("user_friends")->where(['f_uid'=>$v2['id']])->update(['f_role'=>$share_data['role']]);
                        continue;
                    }
                    $in_add = [
                        "user_id"=>  $v['user_id'],
                        "f_uid"=>  $v2['id'],
                        "f_nickname"=>  $v2['nickname'],
                        "f_mobile"=>  $v2['mobile'],
                        "f_status"=>  1,
                        "relation"=>  1,
                        "createtime"=>  time(),
                        "f_avatar"=>  $v2['avatar'],
                        "f_role"=>  $share_data['role'],
                    ];
                    $up_in_arr[] = $in_add;
                }
                Db::name("user_friends")->insertAll($up_in_arr);
            }

        }
        //修改不存在的
        $user_list = Db::name("user")->select();
        $mobile_arr = array_column($user_list, 'mobile');
        Db::name("user_friends")->where("f_mobile",'not in',$mobile_arr)->update(['f_status'=>2,'updatetime'=>time()]);
        echo "添加好友结束";
    }
}
